<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
>
<head th:include="includes/head :: htmlhead(~{::title}, ~{})">
    <meta charset="utf-8">
    <title>用户管理</title>
</head>
<body>
<div id="mainLayout" class="easyui-layout" data-options="fit: true">
    <div style="padding:0px;height:calc(100% - 30px)">
        <table id='dg' style="width:100%;height:100%"></table>
    </div>
    <div id="pg" style="height:30px" class="easyui-pagination">
    </div>
</div>

<!-- 添加模块 -->
<div id="adddlg" class="easyui-dialog" style="width:600px;height:320px;padding:10px 20px"
     closed="true" closable="false" modal="true" buttons="#dlg-buttons">
    <form id="addfm" method="post" novalidate>
        <input name="id" type="hidden"/>
        <table cellpadding="5">
            <tr>
                <td>用户帐号:</td>
                <td><input name="userCode" class="easyui-textbox" type="text" required="true" missingMessage="不能为空"/></td>
                <td>用户姓名:</td>
                <td><input name="userName" class="easyui-textbox" type="text" required="true" missingMessage="不能为空"/></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td>
                    <select id="userSexAdd" class="easyui-combobox" name="userSex" style="width:100%;"
                            data-options="panelHeight:'auto'" required="true" missingMessage="必须选择">
                        <option value="0">男</option>
                        <option value="1">女</option>
                    </select>
                </td>
                <td>用户手机:</td>
                <td><input name="mobileNo" class="easyui-textbox" type="text" required="true" missingMessage="不能为空"/></td>
            </tr>
            <tr>
                <td>用户邮箱:</td>
                <td><input name="mailboxNo" class="easyui-textbox" type="text" required="true"
                           missingMessage="不能为空"/></td>
                <td>用户类型:</td>
                <td>
                    <select id="userTypeAdd" class="easyui-combobox" name="userType" style="width:100%;"
                            data-options="panelHeight:'auto'" required="true" missingMessage="必须选择">
                        <option value="0">普通用户</option>
                        <option value="1">DBA</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>多因子认证:</td>
                <td>
                    <select id="mfaFlagAdd" class="easyui-combobox" name="mfaFlag" style="width:100%;"
                            data-options="panelHeight:'auto'" required="true" missingMessage="必须选择">
                        <option value="0">不开启</option>
                        <option value="1">GoogleAuthenticator认证</option>
                    </select>
                </td>
                <td>用户状态:</td>
                <td>
                    <select id="userStatusAdd" class="easyui-combobox" name="userStatus" style="width:100%;"
                            data-options="panelHeight:'auto'" required="true" missingMessage="必须选择">
                        <option value="0">正常</option>
                        <option value="1">停用</option>
                    </select>
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#adddlg').dialog('close')">取消</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveAddFm()">保存</a>
</div>

<script type="text/javascript">
    let ctxPath = '[[${#servletContext.contextPath}]]';
    $(function () {
        $(window).resize(function () {
            alert(2)
        });

        let toolbar = [{
            text: '添加',
            iconCls: 'icon-diy-add1',
            handler: function () {
                openAddDlg();
            }
        }, {
            text: '删除',
            iconCls: 'icon-diy-delete1',
            handler: function () {
                del();
            }
        }, {
            text: '修改',
            iconCls: 'icon-diy-edit1',
            handler: function () {
                openEditDlg();
            }
        }, {
            text: '重置密码',
            iconCls: 'icon-diy-resetpassword1',
            handler: function () {
                resetPassword();
            }
        }, {
            text: '重置多因子密钥',
            iconCls: 'icon-diy-mfa1',
            handler: function () {
                resetMfaKey();
            }
        }, '-', {
            text: '刷新',
            iconCls: 'icon-diy-refresh1',
            handler: function () {
                pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
            }
        }];
        $('#dg').datagrid({
            data: []
            , columns: [
                [
                    {field: 'id', title: 'id', resizable: true, hidden: true}
                    , {field: 'userCode', title: '登录帐号', resizable: true}
                    , {field: 'userName', title: '用户名称', resizable: true}
                    , {field: 'userSex', title: '用户性别', resizable: true, formatter: formatUserSex}
                    , {field: 'mobileNo', title: '用户手机号', resizable: true}
                    , {field: 'mailboxNo', title: '用户邮箱', resizable: true}
                    , {field: 'userStatus', title: '用户状态', resizable: true, formatter: formatUserStatus}
                    , {field: 'userType', title: '用户类型', resizable: true, formatter: formatUserType}
                    , {field: 'mfaFlag', title: '多因子认证', resizable: true, formatter: formatMfaFlag}
                ]
            ]
            , autoRowHeight: true
            , rownumbers: true
            , singleSelect: true
            , toolbar: toolbar
        });

        $('#pg').pagination({
            pageList: COMMONPARAM_PAGELIST,
            pageSize: COMMONPARAM_DEFAULTPAGESIZE,
            total: 0,
            pageNumber: 1,
            beforePageText: '第',
            afterPageText: '页    共 {pages} 页',
            displayMsg: '当前显示 {from} - {to}条记录,共{total}条记录'
        });

        pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
    });

    function formatUserSex(val, row) {
        if (val == '0') {
            return '男';
        } else if (val == '1') {
            return '女';
        } else {
            return '<span style="color:red;">未知</span>';
        }
    }

    function formatUserStatus(val, row) {
        if (val == '0') {
            return '<span style="color:green;">正常</span>';
        } else {
            return '<span style="color:red;">停用</span>';
        }
    }

    function formatUserType(val, row) {
        if (val == '0') {
            return '普通用户';
        } else if (val == '1') {
            return 'DBA';
        } else if (val == '99') {
            return '管理员';
        } else {
            return '<span style="color:red;">未知</span>';
        }
    }

    function formatMfaFlag(val, row) {
        if (val == '0') {
            return '不开启';
        } else if (val == '1') {
            return 'GoogleAuthenticator认证';
        } else if (val == '2') {
            return '短信认证';
        } else {
            return '<span style="color:red;">未知</span>';
        }
    }

    function pagingList(pageNumber, pageSize) {
        let submitData = {};
        submitData['page'] = pageNumber;
        submitData['limit'] = pageSize;
        postJSON(ctxPath + '/mdmsUser/pagingList', submitData,
            function (data) {
                $('#dg').datagrid({
                    data: data['list']
                })
                $('#pg').pagination({
                    pageList: COMMONPARAM_PAGELIST,
                    pageSize: data['limit'],
                    total: data['total'],
                    pageNumber: data['page'],
                    beforePageText: '第',
                    afterPageText: '页    共 {pages} 页',
                    displayMsg: '当前显示 {from} - {to}条记录,共{total}条记录',
                    onSelectPage: function (pageNumber, pageSize) {
                        pagingList(pageNumber, pageSize);//选择第几页的时候，就刷新
                    },
                });
            }, function (result, resultdesc) {
                $.messager.show({
                    title: '异常',
                    msg: result + '-' + resultdesc,
                    showType: 'show'
                });
            }, function () {
                alert(3)
            }, function () {
                $.messager.progress(COMMONPARAM_PROCESS);
            }, function () {
                $.messager.progress('close');
            });
    }

    function openAddDlg() {
        COMMONPARAM_WEBOPERATION = 'add';
        $('#adddlg').dialog('open').dialog('setTitle', '添加用户');
        $('#addfm').form('clear');
    }

    function openEditDlg() {
        COMMONPARAM_WEBOPERATION = 'edit';
        let row = $('#dg').datagrid('getSelected');
        if (row) {
            $('#adddlg').dialog('open').dialog('setTitle', '修改用户');
            $('#addfm').form('load', row);
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一条记录!',
                showType: 'show'
            });
        }
    }

    function saveAddFm() {
        if (!$('#addfm').form('validate')) {
            return;
        }

        let submitData = geneSubmitDataFromForm("#addfm", {});
        if (COMMONPARAM_WEBOPERATION == 'add') {
            delete submitData['id'];
        }
        postJSON(ctxPath + '/mdmsUser/' + COMMONPARAM_WEBOPERATION, submitData,
            function (data) {
                pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
                $('#adddlg').dialog('close');
            }, function (result, resultdesc) {
                $.messager.show({
                    title: '异常',
                    msg: result + '-' + resultdesc,
                    showType: 'show'
                });
            }, function () {
                alert(3)
            }, function () {
                $.messager.progress(COMMONPARAM_PROCESS);
            }, function () {
                $.messager.progress('close');
            });
    }

    function del() {
        COMMONPARAM_WEBOPERATION = 'del';
        let row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('提示', '你确定要删除该条记录?', function (r) {
                if (r) {
                    let submitData = {};
                    submitData['id'] = row['id'];
                    postJSON(ctxPath + '/mdmsUser/' + COMMONPARAM_WEBOPERATION, submitData,
                        function (data) {
                            pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
                        }, function (result, resultdesc) {
                            $.messager.show({
                                title: '异常',
                                msg: result + '-' + resultdesc,
                                showType: 'show'
                            });
                        }, function () {
                            alert(3)
                        }, function () {
                            $.messager.progress(COMMONPARAM_PROCESS);
                        }, function () {
                            $.messager.progress('close');
                        });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一条记录!',
                showType: 'show'
            });
        }
    }

    function resetPassword() {
        let row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('提示', '你确定要为该用户重置密码?', function (r) {
                if (r) {
                    let submitData = {};
                    submitData['id'] = row['id'];
                    postJSON(ctxPath + '/mdmsUser/resetPassword', submitData,
                        function (data) {
                            $.messager.show({
                                title: '提示',
                                msg: '密码重置成功',
                                showType: 'show'
                            });
                        }, function (result, resultdesc) {
                            $.messager.show({
                                title: '异常',
                                msg: result + '-' + resultdesc,
                                showType: 'show'
                            });
                        }, function () {
                            alert(3)
                        }, function () {
                            $.messager.progress(COMMONPARAM_PROCESS);
                        }, function () {
                            $.messager.progress('close');
                        });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一条记录!',
                showType: 'show'
            });
        }
    }

    function resetMfaKey() {
        let row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('提示', '你确定要为该用户清空MFA密钥?', function (r) {
                if (r) {
                    let submitData = {};
                    submitData['id'] = row['id'];
                    postJSON(ctxPath + '/mdmsUser/resetMfaKey', submitData,
                        function (data) {
                            $.messager.show({
                                title: '提示',
                                msg: '清空MFA密钥成功',
                                showType: 'show'
                            });
                        }, function (result, resultdesc) {
                            $.messager.show({
                                title: '异常',
                                msg: result + '-' + resultdesc,
                                showType: 'show'
                            });
                        }, function () {
                            alert(3)
                        }, function () {
                            $.messager.progress(COMMONPARAM_PROCESS);
                        }, function () {
                            $.messager.progress('close');
                        });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一条记录!',
                showType: 'show'
            });
        }
    }

</script>
</body>